﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;

namespace School.Admin.Admin
{
    public partial class Peronnel : CheckPower
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gridviewbind();
            }
        }
        private void gridviewbind()
        {
            GridView1.DataSource = new BLL.Users().GetList("ID!=1");
            GridView1.DataBind();
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "edit")
            {
                Response.Redirect("EditPeronnel.aspx?id=" + e.CommandArgument);
            }
            if (e.CommandName == "editPWD")
            {
                System.Random a = new Random(System.DateTime.Now.Millisecond);
                int pwd = a.Next(100000, 999999);
                Model.Users u = new BLL.Users().GetModel(int.Parse(e.CommandArgument.ToString()));
                u.MembePassWord =  Encryption.GetMD5(pwd.ToString());
                if (new BLL.Users().Update(u))
                {
                    ScriptHelper.Alert("修改密码成功之路！，请劳记" + pwd + "");
                }

            }
            if (e.CommandName == "del")
            {
                int id = int.Parse(e.CommandArgument.ToString());
                if (new BLL.Users().Delete(id))
                {
                    gridviewbind();
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("EditPeronnel.aspx");
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            gridviewbind();
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#cde9fc'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                Label lbState = (Label)e.Row.FindControl("lbState");
                if (lbState.Text == "1")
                {
                    lbState.Text = "启用";
                }
                else
                {
                    lbState.Text = "禁用";
                }

                Label lbUserType = (Label)e.Row.FindControl("lbUserType");
                lbUserType.Text = new BLL.UserType().GetModel(int.Parse(lbUserType.Text)).TypeName;
            }
        }
    }
}